package com.hivekion.common.socket;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.InetSocketAddress;
import java.util.*;

public class MyWebSocketService extends WebSocketServer {

    public MyWebSocketService(int port) {
        super(new InetSocketAddress(port));
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {

    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        Map<String, List<WebSocket>> mapList = SocketServerUtils.listMap;

        Set<Map.Entry<String, List<WebSocket>>> keySet = mapList.entrySet();
        Iterator<Map.Entry<String, List<WebSocket>>> iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, List<WebSocket>> entry = iterator.next();
            String liveId = entry.getKey();
            List<WebSocket> socketList = entry.getValue();
            if (StringUtils.isNoneBlank(liveId) && socketList != null && socketList.size() > 0) {
                for (WebSocket socket :
                        socketList) {
                    if (socket.equals(conn)) {
                        socketList.remove(socket);
                    }
                }
                SocketServerUtils.listMap.put(liveId, socketList);
            }
        }
    }

    @Override
    public void onMessage(WebSocket socket, String message) {
        if (message.contains("type") && message.contains("liveId")) {
            SocketReceiveModel receiveModel = JSON.parseObject(message, SocketReceiveModel.class);
            if (receiveModel != null) {
                if (receiveModel.getType().equals(0)) {
                    List<WebSocket> list = SocketServerUtils.listMap.get(receiveModel.getLiveId());
                    if (list == null) {
                        list = new ArrayList<>();
                    }
                    if (!list.contains(socket)) {
                        list.add(socket);
                    }
                    SocketServerUtils.listMap.put(receiveModel.getLiveId(), list);
                }
                if (receiveModel.getType().equals(1)) {
                    List<WebSocket> list = SocketServerUtils.listMap.get(receiveModel.getLiveId());
                    if (list != null && list.size() > 0) {
                        for (WebSocket ket :
                                list) {
                            if (ket != null) {
                                if (!ket.equals(socket) && ket.isOpen()) {
                                    ket.send(message);
                                }
                            } else {
                                list.remove(receiveModel.getLiveId());
                                continue;
                            }
                        }
                    }
                }
            }
        }
        System.out.println("收到消息：" + message);
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {

    }

    @Override
    public void onStart() {

    }

}
